<template>
  <el-drawer
      v-model="isShow"
      size="50%"
      :destroy-on-close="true"
      @close="closeHandle"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :show-close="false"
  >
  <template #header="{ close, titleId, titleClass }">
          <h4 :id="titleId" :class="titleClass">{{ title }}</h4>
      </template>
  <el-form :model="form"  ref="formRef" :rules="rules" v-loading="loading">
      <el-row class="mb-4" :gutter="20">
          <el-col :span="24">
              <el-form-item label="开始时间" label-width="80px" required prop="startdate">
                  <el-date-picker
                  v-model="form.startdate"
                  type="datetime"
                  placeholder="请选择开始时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  style="width:100%"
                  :readonly="isreadonly"
              />
              </el-form-item>
          </el-col>
          <el-col :span="24">
              <el-form-item label="结束时间" label-width="80px" required prop="enddate">
                  <el-date-picker
                  v-model="form.enddate"
                  type="datetime"
                  placeholder="请选择结束时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  style="width:100%"
                  :readonly="isreadonly"
              />
              </el-form-item>
          </el-col>
          <el-col :span="24">
              <el-form-item label="特情情况" label-position="top" required prop="dutydetail">
                  <el-input v-model="form.dutydetail" type="textarea" :rows="3" :readonly="isreadonly" />
              </el-form-item>
          </el-col>
      </el-row>
  </el-form>
  <template #footer>
      <el-button type="primary" v-if="!isreadonly" @click="Confirm">确认</el-button>
      <el-button @click="isShow = false">关闭</el-button>
  </template>
  {{  }}
  </el-drawer>
</template>
<script setup>

import {getCurrentInstance} from "vue";
const {proxy} = getCurrentInstance()
import {DutyRecordAdd,DutyRecordInfo,DutyRecordEdit} from "@/api/ApplyDetail/ApplyDetail";
const {nj_sex,nj_sheanren_type,nj_sheanren_status} = proxy.useDict( 'nj_sex','nj_sheanren_type','nj_sheanren_status')

const isShow = ref(false)
const loading = ref(false)
const isreadonly = ref(false)
const user_name = ref('')

const title = ref('新增特情登记')
const form = ref({
})
const changeShowStatus = () => {
  isShow.value = !isShow.value
}
const rules = {
  startdate: [{ required: true, message: "执勤开始时间不能为空", trigger: "blur" }],
  edddate: [{ required: true, message: "执勤开始时间不能为空", trigger: "blur" }],
  dutydetail: [{ required: true, message: "特情情况不能为空", trigger: "blur" }],
}
// 通知事件
const emit = defineEmits(['getList'])
onMounted(() => {
  
})
// 获取详情
const getInfo = async (jwApplydetailId) => {
  await DutyRecordInfo(jwApplydetailId).then(res => {
    form.value = res.data
    loading.value = false
  })
}
const showAdd = async (jwApplydetailId,data) => {
  changeShowStatus()
  console.log("show新增",jwApplydetailId,data)
  form.value.jwApplydetailId = jwApplydetailId
}

const showEdit = async (data,isSee,username) => {
  changeShowStatus()
  if(isSee == 'See'){
    title.value = '违规违纪详情'
      isreadonly.value = true
      loading.value = true
      getInfo(data.jwDutyrecordId)
  }else{
    title.value = '违规违纪编辑'
      isreadonly.value = false
      loading.value = true
      getInfo(data.jwDutyrecordId)
  }
  
}
// 确认按钮
const Confirm = async () => {
  proxy.$refs["formRef"].validate(valid => {
  if (valid) {
      if(form.value['jwDutyrecordId']){
          loading.value = true
          DutyRecordEdit(form.value).then(res => {
              loading.value = false
              changeShowStatus()
              emit('getList', '来了老弟')
          })
      } else {
        DutyRecordAdd(form.value).then(res => {
              loading.value = false
              changeShowStatus()
              proxy.$modal.msgSuccess(`新增成功`)
              emit('getList', '来了老弟')
          })
      }
  }
});
}

defineExpose({showAdd,showEdit})

// 关闭后的回调事件
const closeHandle = () => {
  form.value = {}
  emit('getList', '来了来了')
  console.log(123123)
}
</script>
<style>

</style>